package com.bfsuolframework.core.dao.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.hibernate.event.internal.DefaultSaveOrUpdateEventListener;
import org.hibernate.event.spi.SaveOrUpdateEvent;

import com.bfsuolframework.core.configuration.FilePropertyConfigLoader;

public class EncryptSaveListener extends DefaultSaveOrUpdateEventListener  {

	private static final long serialVersionUID = 1L;

	private Map<String,String> encrypts = new HashMap<String,String>();
	
	public EncryptSaveListener(){
		try {
			Properties props = new FilePropertyConfigLoader("/config/encrypt.properties").load();
			for(String clazz:props.stringPropertyNames()){
				String methods = props.getProperty(clazz);
				if(methods != null && !methods.equals("")){
					encrypts.put(clazz, ","+methods+",");
				}
			}
		} catch (Exception e) {
			//如果读取失败，则不处理
		}
	}
	
	public void onSaveOrUpdate(SaveOrUpdateEvent event){
		for(String clazz : encrypts.keySet()){
    		if (event.getObject().getClass().getName().equals(clazz)) {
    			//TODO 通过反射得到数据，加密后，再写回去
    			//String value = EncryptUtils.DESEncode((String)state[index]);
    		}
    	}
	}
}
